package com.mark.org.service.impl;

import com.mark.basic.service.Impl.BaseServiceImpl;
import com.mark.basic.util.BaiduAuditUtils;
import com.mark.basic.util.BusinessException;
import com.mark.org.domain.Employee;
import com.mark.org.domain.Shop;
import com.mark.org.domain.ShopAuditLog;
import com.mark.org.mapper.EmployeeMapper;
import com.mark.org.mapper.ShopAuditLogMapper;
import com.mark.org.mapper.ShopMapper;
import com.mark.org.service.IEmployeeService;
import com.mark.org.service.IShopService;
import com.mark.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IEmployeeService employeeService;
    private ShopAuditLogMapper shopAuditLogMapperMapper;
    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public void settlement(Shop shop) {

        // 判断店铺为空是抛出一个业务异常
        String name = shop.getName();
        if(name== null){
            throw new BusinessException("店铺名不能为空！！！！");
        }
        // 非法校验，智能审核
        if (!BaiduAuditUtils.TextCensor(name) ){
            throw new BusinessException("涉嫌违规文字，请重新填写！！");
        }
        // 判断店铺是否被注册
        // 获取店铺的名称
        Shop shopTmp = shopMapper.listName(shop);
        if (shopTmp !=null){
            throw new BusinessException("店铺名已重复！！！！");
        }else {
            shopMapper.add(shopTmp);
        }


        // 获取店长
        // 获取前端传过来的employee的信息
        Employee admin = shop.getAdmin();
//        System.err.println(admin);
        // 获取店长名字是否存在于员工中
        Employee empTmp = employeeMapper.listUserName(admin.getUsername());
        // 判断店长是否存在和是否已经注册过店铺
        if (empTmp != null && empTmp.getShop_id() != null){
            throw new BusinessException("店长名已重复，请重新输入！！！！");
        }
        if (empTmp == null){
//            employeeMapper.add(admin);
            /*employeeMapper.save(admin);//返回自增Id
             * 这里不应该直接调用别的业务实体的mapper操作数据
             * 而应该将对应的实体业务交给对应的service实现类去操作
             * */
            employeeService.add(admin); // 高级封装
            empTmp = admin;
        }

        // 保存店铺信息
        shop.setAdmin_id(empTmp.getId());
        shopMapper.add(shop);
        // 约束条件：店长只能有一个店铺，所以添加的店长名字在员工中以存在
        empTmp.setShop_id(shop.getId());
        employeeMapper.update(empTmp);
    }

        /**
         * 通过
         * @param shopAuditLog
         */
        @Override
        public void pass(ShopAuditLog shopAuditLog) {
            // 1、修改状态
            Long shopId = shopAuditLog.getShop_id(); // 获取审核的id
            Shop shop = shopMapper.listById(shopId); // 通过id获取店铺信息
            shop.setState(1);// 将店铺状态改为审核通过
            shopMapper.update(shop);// 修改店铺信息

            // 2、添加日志（日志为审核次数加信息）
            // 获取审核人信息 先写死 @TODO
//            Long auditId = shopAuditLog.getAuditId();
            shopAuditLog.setAudit_id(325L);
            shopAuditLogMapperMapper.add(shopAuditLog);
            // 3、获取店长信息发邮件
            Long admin_id = shop.getAdmin_id(); // 获取店长id
            Employee employee = employeeMapper.listById(admin_id); //获取店长信息

            // 4、发邮件通知审核通过
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            //设置发送人
            mailMessage.setFrom("1347403676@qq.com");
            //邮件主题
            mailMessage.setSubject("店铺入驻");
            //邮件内容
            mailMessage.setText("您的店铺审核已通过，可以使用啦！！");
            //收件人
//            mailMessage.setTo(employee.getEmail());
            mailMessage.setTo("1347403676@qq.com");
            javaMailSender.send(mailMessage);

        }

    @Override
    public void reject(ShopAuditLog shopAuditLog) {
        // 1、修改状态
        Long shopId = shopAuditLog.getShop_id(); // 获取审核的id
        Shop shop = shopMapper.listById(shopId); // 通过id获取店铺信息
        shop.setState(-1);// 将店铺状态改为审核未通过
        shopMapper.update(shop);// 修改店铺信息

        // 2、添加日志（日志为审核次数加信息）
        // 获取审核人信息 先写死 @TODO
//            Long auditId = shopAuditLog.getAuditId();
        shopAuditLog.setAudit_id(325L);
        shopAuditLogMapperMapper.add(shopAuditLog);

        // 3、获取店长信息发邮件
        Long admin_id = shop.getAdmin_id(); // 获取店长id
        Employee employee = employeeMapper.listById(admin_id); //获取店长信息

        // 4、发邮件通知审核被驳回
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            //发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("1347403676@qq.com");
            //设置邮件主题
            helper.setSubject("审核失败通知");
            //邮件正文
            helper.setText("<h1>审核失败</h1><br>"+
                    "<span>失败原因："+shopAuditLog.getNote()+"</span><br>"+
                    "<a href='http://127.0.0.1:8081/#/register2?id="+shop.getId()+"'>点击修改资料</a>",true);
            //收件人
            helper.setTo("1347403676@qq.com");
            //发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<ShopVo> queryByState() {
        return shopMapper.queryByState();
    }

    @Override
    public void batchSave(List<Shop> shops) {
            shopMapper.batchSave(shops);
    }
}